package com.ncs.base.task.quartz;

import java.util.Date;

import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Component;

import com.ncs.base.task.config.UserJob;
import com.ncs.base.task.service.UserMasterService;
import com.ncs.base.task.service.UserSalveService;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import lombok.extern.slf4j.Slf4j;

@EnableAsync
@Component
@Slf4j
public class MyScheduler {

	 public static void main(String[] args) throws SchedulerException, InterruptedException {
			String table = null;
			String flag = "1";
			if("1".equals(flag)) {
				table ="user";
			}else {
				table = "user_01";
			}
			
			
		/*
		 * SpringApplication.run(MyScheduler.class, args); ApplicationContext context =
		 * SpringUtil.getApplicationContext(); UserMasterService userMasterService =
		 * context.getBean(UserMasterService.class); //你的ado或service UserSalveService
		 * userSalveService = context.getBean(UserSalveService.class);
		 */    
			
		 // 1、创建调度器Scheduler
	        SchedulerFactory schedulerFactory = new StdSchedulerFactory();
	        Scheduler scheduler = schedulerFactory.getScheduler();
	        // 2、创建JobDetail实例，并与PrintWordsJob类绑定(Job执行内容)
	        JobDetail jobDetail = JobBuilder.newJob(UserJob.class)
	                .usingJobData("jobDetail1", "这个Job用来测试的")
	                .usingJobData("tableName", table)
	                .withIdentity("job1", "group1").build();
	        // 3、构建Trigger实例,每隔1s执行一次
	        Date startDate = new Date();
	        startDate.setTime(startDate.getTime() + 5000);

	        Date endDate = new Date();
	        endDate.setTime(startDate.getTime() + 5000);

	        CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "triggerGroup1")
	                .usingJobData("trigger1", "这是jobDetail1的trigger")
	                .usingJobData("tableName", table)
	                .startNow()//立即生效
	                .startAt(startDate)
	                .endAt(endDate)
	                .withSchedule(CronScheduleBuilder.cronSchedule("*/5 * * * * ?"))
	                .build();

	        //4、执行
	        scheduler.scheduleJob(jobDetail, cronTrigger);
	        System.out.println("--------scheduler start ! ------------");
	        scheduler.start();
	        System.out.println("--------scheduler shutdown ! ------------");

	    }
	
}
